﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using GameLogic.Controls;
using GameLogic.Logic;

namespace Demo_End
{
    public partial class GameContent : UserControl, IGameFeatures
    {
        GameLoop gameLoop; // GameLoop behavior for collision and animation

        MovingCollidableBehavior ballBehavior;

        public GameContent()
        {
            // Required to initialize variables
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Find the GameLoop
            GameLoopFinder glf = new GameLoopFinder(LayoutRoot);
            gameLoop = glf.GameLoop;


            // Find ballBehavior
            BehaviorCollection behaviors = Interaction.GetBehaviors(_ball);
            this.ballBehavior = (MovingCollidableBehavior)(from behavior in behaviors
                                                           where typeof(MovingCollidableBehavior).IsAssignableFrom(behavior.GetType())
                                                           select behavior).FirstOrDefault();

        }


        public string CompanyName
        {
            get { return "Microsoft Deutschland GmbH"; }
        }

        public string Description
        {
            get { return String.Empty; }
        }

        public string DeveloperName
        {
            get { return "Dennis Sasse"; }
        }

        public string GameName
        {
            get { return "SillyTestGame"; }
        }

        public Uri SettingsPage
        {
            get { return null; }
        }

        public bool IsLifeBased
        {
            get { return true; }
        }

        public bool IsTimeBased
        {
            get { return false; }
        }

        public TimeSpan GameDuration
        {
            get { return new TimeSpan(); }
        }

        public event EventHandler<GameOverEventArgs> GameOver;
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        public event EventHandler<ScoresChangedEventArgs> ScoresChanged;
        public event EventHandler<TimeChangedEventArgs> TimeChanged;

        public void OnLivesChanged(int lives)
        {
            // Fire LivesChanged event
            if (LivesChanged != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Lives = lives;
                LivesChanged(this, args);
            }
        }

        public void OnScoresChanged(int scores)
        {
            // Fire ScoresChanged event
            if (ScoresChanged != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Scores = scores;
                ScoresChanged(this, args);
            }

            // Increase ball velocity
            if (ballBehavior.Velocity.Length < 15.0)
            {
                ballBehavior.Velocity =  ballBehavior.Velocity * 1.1;
            }
        }

        public void Start()
        {
            if (gameLoop != null)
            {
                // Start GameLoop
                gameLoop.StartGameLoop();
            }
        }

        public void Stop()
        {
            if (gameLoop != null)
            {
                // Stop GameLoop and reset collision elements
                gameLoop.ResetGameLoop();
            }
        }

        public void Pause()
        {
            if (gameLoop != null)
            {
                // Pause GameLoop
                gameLoop.PauseGameLoop();
            }
        }

        public void Resume()
        {
            if (gameLoop != null)
            {
                // Resume GameLoop
                gameLoop.StartGameLoop();
            }
        }

    }
}